﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Task.Data;
using CentralOffice.Task.BusinessActions;

using ApplicationTable = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsDataTable;
using ApplicationRow = CentralOffice.Task.Data.ApplicationDataSet.ApplicationsRow;

using ApprovalTable = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsDataTable;
using ApprovalRow = CentralOffice.Task.Data.ApprovalDataSet.ApprovalsRow;

using CentralOffice.MyTable.Data;
using CentralOffice.MyTable.BusinessActions;
using Husb.Security;
using CentralOffice.Admin.BusinessActions;

using SmsTable = CentralOffice.MyTable.Data.SmsDataSet.smsDataTable;
using SmsRow = CentralOffice.MyTable.Data.SmsDataSet.smsRow;
using SmsBodyTable = CentralOffice.MyTable.Data.SmsDataSet.sms_bodyDataTable;
using SmsBodyRow = CentralOffice.MyTable.Data.SmsDataSet.sms_bodyRow;
using System.Collections;
using System.Data;

namespace CentralOffice.WebClient.Tasks
{
    public partial class ApplicationExecute : PageBase
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlCategory, SiteConstants.ApprovalObject, true);
                ddlCategory.SelectedIndex = 0;
            }
        }

        protected void ods_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

            IOrderedDictionary parameters = e.InputParameters;
            //ApplicationRow application = (new ApplicationDataSet()).Applications.NewApplicationsRow();
            //application.Applicant = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
            int status = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "通过");
            int exe = EnumUtil.GetInt32(SiteConstants.ApprovalStatus, CategoryDataSourceType.Xml, "已执行");
            if (parameters["whereClause"] == null || parameters["whereClause"].ToString().Length == 0)
            {
                parameters["whereClause"] = " (Status = " + status.ToString() + " OR Status = " + exe.ToString() + ")" + " AND  ApplicationExecutor LIKE '%" + CommonUtil.TrimAll(ApplicationContext.ActualUserName) + "%'";
            }
            else
            {
                parameters["whereClause"] = parameters["whereClause"].ToString() + " AND (Status = " + status.ToString() + " OR Status = " + exe.ToString() +  ")" + " AND ApplicationExecutor LIKE '%" + CommonUtil.TrimAll(ApplicationContext.ActualUserName) + "%'";
            }
            // application.FilterExpression;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            ApplicationRow application = (new ApplicationDataSet()).Applications.NewApplicationsRow();

            application.Name = txtName.Text;
            if (ddlCategory.SelectedIndex > 0)
            {
                application.Category = Int32.Parse(ddlCategory.SelectedItem.Value);
            }
            application.Applicant = txtApplicant.Text;

            if (txtDateOfApplication.Text.Length > 0)
            {
                application.DateOfApplication = DateTime.Parse(txtDateOfApplication.Text);
            }
            if (txtDateOfApplicationEnd.Text.Length > 0)
            {
                application.DateOfApplicationEnd = DateTime.Parse(txtDateOfApplicationEnd.Text);
            }
            //task.CloseDate = txtCloseDate.Text;
            //task.Profile = txtProfile.Text;
            application.ApplicationExecutor = CommonUtil.TrimAll(ApplicationContext.ActualUserName);

            odsApplication.SelectParameters.Clear();
            odsApplication.SelectParameters.Add("whereClause", application.FilterExpression);
            //odsTask.SelectParameters["whereClause"].DefaultValue = task.FilterExpression;//companyInfo.FilterExpression
            //CentralOffice.Task.BusinessActions.Applications.DeleteById(new Guid("d6fefe93-e0e2-4b0f-b2f8-3522be7533e3"), true, true);
        }


        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApplicationRow row = (e.Row.DataItem as DataRowView).Row as ApplicationRow;
                if (!row.IsApplicationCreatedByNull() && row.ApplicationCreatedBy != row.Applicant)
                {
                    //e.Row.Style.Add(HtmlTextWriterStyle.Color, "#483D8B");
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        cell.Style.Add(HtmlTextWriterStyle.Color, "#483D8B");
                    }
                }

                //TableCell statusCell = e.Row.Cells[5];
                //if (statusCell.Text == "通过")
                //{
                //    statusCell.Style.Add(HtmlTextWriterStyle.Color, "#0f0");
                //}

                SiteUtil.SetStatusColor(e, 6);


            }
        }
    }
}
